package bt;

/**
 * Projeto : Arvore Preta e Vermelha
 * 
 * Equipe:
 * 			Antonio Eduardo
 * 			Gabriel de Brito
 * 			Gervasio Costa
 * 			Jordan Ferreira
 * 			Wesley Breno
 * 
 */

public class BTNode<T> {
	protected T data;
	protected BTNode<T> left;
	protected BTNode<T> right;
	protected BTNode<T> parent;

	public BTNode() {
		data = null;
		left = null;
		right = null;
		parent = null;
	}

	public boolean isEmpty() {
		return this.data == null;
	}

	@Override
	public String toString() {
		String resp = "NIL";
		if (!isEmpty()) {
			resp = data.toString();
		}
		return resp;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		boolean resp = false;
		if (obj instanceof BTNode) {
			resp = this.isEmpty() && ((BTNode<T>) obj).isEmpty();
			if (!resp) {
				if (!(!this.isEmpty() && ((BTNode<T>) obj).isEmpty() || this
						.isEmpty() && !((BTNode<T>) obj).isEmpty()))
					resp = this.data.equals(((BTNode<T>) obj).data);
			}
		}
		return resp;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public boolean isLeaf() {
		return getLeft().isEmpty() && getRight().isEmpty();
	}

	public BTNode<T> getAvo() {
		BTNode<T> resp = new BTNode<T>();
		if (!getParent().isEmpty()) {
			resp = getParent().getParent();
		}
		return resp;
	}

	public BTNode<T> getTio() {
		BTNode<T> resp = getAvo();
		if (!resp.isEmpty()) {
			if (resp.getLeft().equals(getParent()))
				resp = resp.getRight();
			else
				resp = resp.getLeft();
		}
		return resp;
	}

	public BTNode<T> getIrmao() {
		BTNode<T> resp = new BTNode<T>();
		if (!getParent().isEmpty()) {
			if (getParent().getLeft().getData().equals(this.data))
				resp = resp.getRight();
			else
				resp = resp.getLeft();
		}
		return resp;
	}

	public BTNode<T> getLeftSobrinho() {
		BTNode<T> resp = getIrmao();
		if (!resp.isEmpty()) {
			resp = resp.getLeft();
		}
		return resp;
	}

	public BTNode<T> getRightSobrinho() {
		BTNode<T> resp = getIrmao();
		if (!resp.isEmpty()) {
			resp = resp.getRight();
		}
		return resp;
	}

	public BTNode<T> getLeft() {
		return left;
	}

	public void setLeft(BTNode<T> left) {
		this.left = left;
	}

	public BTNode<T> getRight() {
		return right;
	}

	public void setRight(BTNode<T> right) {
		this.right = right;
	}

	public BTNode<T> getParent() {
		return parent;
	}

	public void setParent(BTNode<T> parent) {
		this.parent = parent;
	}

}